package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.SpecDao;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import com.sun.org.apache.xpath.internal.objects.XObject;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class SpecServiceImpl extends ServiceImpl<SpecDao,Spec> implements SpecService {

    @Autowired
    private SpecDao specDao;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Result findAll(Integer id) {
        List<Spec> all = specDao.findAll(id);
        System.out.println(all);
        return new Result(true, 20004,"查询成功",all);
    }

    /**
     * 根据添加规格
     * @param
     */
    @Override
    public void findadd(Spec spec) {
        baseMapper.insert(spec);

    }

    /**
     * 根据id更新
     * @param id
     * @return
     */
    @Override
    public Result update(Integer id,Spec spec) {

        QueryWrapper<Spec> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int count = baseMapper.update(spec, wrapper);
        return new Result(true, StatusCode.OK,"更新成功",count);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void Delete(Integer id) {
        specDao.Delete(id);
        System.out.println("删除成功");


    }

    @Override
    public Result findPage(Integer page, Integer size) {
        //todo: 分页设置
        IPage iPage = new Page(page,size);


        //todo: 分页查询结果
        IPage pageSelectResult = baseMapper.selectPage(iPage,null);

        if (pageSelectResult.getRecords() == null) {
            return new Result(false,404,"无结果",null);
        }

        //todo: 封装结果
        PageResult<Category> pageResult = new PageResult<>();
        pageResult.setTotal(pageSelectResult.getTotal());
        pageResult.setRows(pageSelectResult.getRecords());


        //todo: 返回结果
        return new Result(true,200,null,pageResult);
    }
}
